/*

  json_number.h
  Shan.Lib

 Copyright (c) 2013, Sung-Han Park
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:

 1. Redistributions of source code must retain the above copyright notice, this
 list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright notice,
 this list of conditions and the following disclaimer in the documentation
 and/or other materials provided with the distribution.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#ifndef _Shan_JSON_json_number_h_
#define _Shan_JSON_json_number_h_

#include "json_value.h"

namespace shan {
	namespace json {
		class number : public json::value {
		public:
			number() :
			flag(INTEGER), lval(0) {
			}

			number(long long num) :
			flag(INTEGER), lval(num) {
			}

			number(double num) :
			flag(DOUBLE), dval(num) {
			}

			virtual ~number() {}

			virtual int to_int() {
				return static_cast<int>(to_long_long());
			}

			virtual long to_long() {
				return static_cast<long>(to_long_long());
			}

			virtual long long to_long_long() {
				if (flag == INTEGER)
					return lval;
				else
					return static_cast<long long>(dval);
			}

			virtual float to_float() {
				return static_cast<float>(to_double());
			}

			virtual double to_double() {
				if (flag == INTEGER)
					return static_cast<double>(lval);
				else
					return dval;
			}

			virtual bool to_bool() {
				return (to_long_long() != 0);
			}

			virtual bool is_null() {
				return false;
			}

			virtual std::string to_json() const;
			virtual std::string to_string() const {
				return to_json();
			}

		protected:
			virtual void internal_parse(std::istream& is);

		protected:
			static const char INTEGER = 0;
			static const char DOUBLE = 1;
			
			char  flag;
			union {
				long long lval;
				double dval;
			};
		};
	}
}

#endif
